import { service } from '@vimi/utils-api'

const state = {
  enumOrgClass: [],
  enumOrgClassObj: {},
}

const getters = {
  enumOrgClass: state => state.enumOrgClass,
  enumOrgClassObj: state => state.enumOrgClassObj,
}

const actions = {
  async getEnumOrgClass({ commit }, force = false) {
    if (!force && state.enumOrgClass.length) {
      return Promise.resolve(state.OrgClass)
    }

    const res = await service.get('/auth/org/class/?orgClassType=1')
    const data = (res && res.data) || []
    const obj = data.reduce((a, b) => {
      a[b.orgClass] = b.orgClassName
      return a
    }, {})
    commit('enumOrgClassObj', obj)
    commit('enumOrgClass', data)
    return data
  },
}

const mutations = {
  enumOrgClass: (state, payload) => (state.enumOrgClass = payload),
  enumOrgClassObj: (state, payload) => (state.enumOrgClassObj = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
